package com.xj.zhihudemo.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flipboard.bottomsheet.BottomSheetLayout;
import com.flipboard.bottomsheet.commons.IntentPickerSheetView;
import com.xj.zhihudemo.R;
import com.xj.zhihudemo.ui.fragment.StoryFragment;
import com.xj.zhihudemo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Comparator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.xj.zhihudemo.R.id.viewPager;


public class WebActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private final String TAG = getClass().getSimpleName();
    Context mContext;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.story_content)
    FrameLayout mStoryContent;
    @BindView(R.id.menu_item_back)
    TextView mMenuItemBack;
    @BindView(R.id.menu_item_share)
    TextView mMenuItemShare;
    @BindView(R.id.menu_item_collect)
    TextView mMenuItemCollect;
    @BindView(R.id.menu_item_comment)
    TextView mMenuItemComment;
    @BindView(R.id.menu_item_comment_num)
    TextView mMenuItemCommentNum;
    @BindView(R.id.menu_item_praise)
    TextView mMenuItemPraise;
    @BindView(R.id.menu_item_praise_num)
    TextView mMenuItemPraiseNum;
    @BindView(R.id.bar)
    RelativeLayout mBar;
    @BindView(R.id.bottom_sheet)
    BottomSheetLayout mBottomSheet;
    private int current_story_id;
    private int index;
    private ArrayList<Integer> mIdList;
    private ArrayList<Integer> readIdList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        mContext = this;
        initData();
//        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    private void initData() {
        mIdList = getIntent().getIntegerArrayListExtra("ids");
        index = getIntent().getIntExtra("index", 0);
        current_story_id = mIdList.get(index);
        Log.e(TAG, "index:" + index);
        Log.e(TAG, "story_id:" + current_story_id);

        readIdList.add(current_story_id);

    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(viewPager);

        mViewPager.addOnPageChangeListener(this);
        mViewPager.setAdapter(new DetailAdapter(getSupportFragmentManager(), mIdList));
        mViewPager.setCurrentItem(index);
        mViewPager.setOffscreenPageLimit(4);

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        current_story_id = mIdList.get(position);
        Log.e(TAG, current_story_id + "");
        int story_id = mIdList.get(position);
        if (!readIdList.contains(story_id)) {
            readIdList.add(story_id);
        }


    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @OnClick({R.id.menu_item_back, R.id.menu_item_share, R.id.menu_item_collect, R.id.menu_item_comment, R.id.menu_item_comment_num, R.id.menu_item_praise})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu_item_back:
                finish();
                break;
            case R.id.menu_item_share:
                ToastUtils.with(this).show("点击分享");
                share();
                break;
            case R.id.menu_item_collect:
                ToastUtils.with(this).show("点击收藏");
                break;
            case R.id.menu_item_comment:
                ToastUtils.with(this).show("点击评论");
                break;
            case R.id.menu_item_comment_num:
                ToastUtils.with(this).show("点击分享");
                break;
            case R.id.menu_item_praise:
                ToastUtils.with(this).show("点击赞");
                break;
        }
    }

    private void share() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

//        imm.hideSoftInputFromWindow(shareText.getWindowToken(), 0);
        final Intent shareIntent = new Intent(Intent.ACTION_SEND);
//        shareIntent.putExtra(Intent.EXTRA_TEXT, shareText.getText() + "");
        shareIntent.setType("text/plain");

        IntentPickerSheetView intentPickerSheet = new
                IntentPickerSheetView(getApplication(), shareIntent, "分享到..."
                , new IntentPickerSheetView.OnIntentPickedListener() {
            @Override
            public void onIntentPicked(IntentPickerSheetView.ActivityInfo activityInfo) {
                mBottomSheet.dismissSheet();
                startActivity(activityInfo.getConcreteIntent(shareIntent));
            }
        });

        // 过滤到像系统蓝牙这样的分享模式（可以过滤掉也可以不用过滤）
                /*
                intentPickerSheet.setFilter(new IntentPickerSheetView.Filter() {
                    @Override
                    public boolean include(IntentPickerSheetView.ActivityInfo info) {
                        return !info.componentName.getPackageName().startsWith("com.android");
                    }
                });
                */

        // 排序分享模块
        intentPickerSheet.setSortMethod(new Comparator<IntentPickerSheetView.ActivityInfo>() {
            @Override
            public int compare(IntentPickerSheetView.ActivityInfo lhs, IntentPickerSheetView.ActivityInfo rhs) {
                return rhs.label.compareTo(lhs.label);
            }
        });

        // 增加一个自定义的分享选择方式（可选）
//        Drawable customDrawable = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
//        IntentPickerSheetView.ActivityInfo customInfo = new IntentPickerSheetView.ActivityInfo(customDrawable, "zhang phil custom", getApplicationContext(), Main2Activity.class);
//        intentPickerSheet.setMixins(Collections.singletonList(customInfo));

        mBottomSheet.showWithSheetView(intentPickerSheet);
    }

    class DetailAdapter extends FragmentPagerAdapter {

        private ArrayList<Integer> idList;

        public DetailAdapter(FragmentManager fm, ArrayList<Integer> data) {
            super(fm);
            this.idList = data;
        }

        @Override
        public Fragment getItem(int position) {
            StoryFragment fragment = new StoryFragment();
            Bundle bundle = new Bundle();
//            bundle.putInt("news_id", current_story_id);
            bundle.putInt("news_id", idList.get(position));
            Log.e(TAG, "选择的页面" + idList.get(position));
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public int getCount() {
            return idList.size();
        }
    }


}
